package com.lrkj.web.phone;

import com.lrkj.common.component.WeChatComponent;
import com.lrkj.common.constant.Constants;
import com.lrkj.common.core.controller.BaseController;
import com.lrkj.common.core.domain.AjaxResult;
import com.lrkj.common.core.domain.entity.SysUser;
import com.lrkj.common.core.domain.model.LoginBody;
import com.lrkj.framework.security.MiniContextUtils;
import com.lrkj.framework.web.service.SysLoginService;
import com.lrkj.phone.service.PhoneLoginService;
import com.lrkj.phone.vo.PhoneUserVo;
import com.lrkj.phone.vo.RegisterVo;
import com.lrkj.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 登录、注册、发送验证码
 */
@RestController
@Api(tags = "小程序 登录、注册、发送验证码")
public class PhoneLoginController extends BaseController {

    @Autowired
    private PhoneLoginService loginService;


    @Autowired
    private WeChatComponent weChatComponent;


    @Autowired
    private ISysUserService userService;

    @Autowired
    private SysLoginService sysLoginService;

    /**
     * app , 小程序登录按钮
     *
     * @param loginBody 登录Dto
     * @return 登录结果
     */
    @PostMapping("/phone/login")
    @ApiOperation(value = "小程序登录按钮", notes = "小程序登录按钮")
    public AjaxResult weChatLogin(@RequestBody PhoneUserVo loginBody) {
        PhoneUserVo login = loginService.login(loginBody);
        return AjaxResult.success(login);
    }

    /**
     * 注册用户
     *
     * @param registerVo 注册信息
     * @return 注册后信息
     */
    @PostMapping("/phone/register")
    @ApiOperation(value = "注册用户", notes = "注册用户")
    public AjaxResult register(@RequestBody @Validated RegisterVo registerVo) {
        return AjaxResult.success(loginService.register(registerVo));
    }

    /**
     * 发送验证码
     *
     * @param phoneNum 手机号l
     * @return 发送结果
     */
    @GetMapping("/phone/sendCode/{phoneNum}")
    @ApiOperation(value = "发送验证码", notes = "发送验证码")
    public AjaxResult sendCode(@PathVariable("phoneNum") String phoneNum) {
        return AjaxResult.success(loginService.sendCode(phoneNum));
    }

    /**
     * 修改用户信息
     *
     * @param userVo 用户信息
     * @return 注册后信息
     */
    @PostMapping("/phone/editAppUser")
    @ApiOperation(value = "修改用户信息", notes = "修改用户信息")
    public AjaxResult editAppUser(@RequestBody @Validated PhoneUserVo userVo) {
        return AjaxResult.success(loginService.editAppUser(userVo));
    }


    /**
     * 获取app用户详细信息
     */
    @GetMapping(value = "/phone/getAppUserInfo/{openId}")
    @ApiOperation(value = "获取app用户详细信息", notes = "获取app用户详细信息")
    public AjaxResult getAppUserInfo(@PathVariable("openId") String openId) {
        return AjaxResult.success(loginService.getAppUserInfo(openId));
    }

    /**
     * 获取当前登录用户详细信息
     */
    @GetMapping(value = "/phone/getCurrentAppUser")
    @ApiOperation(value = "获取当前登录用户详细信息", notes = "获取当前登录用户详细信息")
    public AjaxResult getAppUser() {
        String phoneNum = MiniContextUtils.getPhoneNum();
        return AjaxResult.success(loginService.getAppUserInfo(phoneNum));
    }

    /**
     * 管理端,微信用户认证
     *
     * @param loginBody 登录Dto
     * @return 登录结果
     */
    @PostMapping("/phone/wxVerify")
    @ApiOperation(value = "管理端,微信用户认证", notes = "管理端,微信用户认证")
    public AjaxResult wxVerify(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        String openId = weChatComponent.getOpenIdByCode(loginBody.getCode());
        SysUser sysUser = userService.selectUserByOpenId(openId);
        //如果已经存在openId 就系统进行登录
        if (null != sysUser) {
            String token = sysLoginService.login(sysUser.getUserName(), sysUser.getPassword(), loginBody.getCode(),
                    loginBody.getUuid());
            ajax.put(Constants.TOKEN, token);
        }
        ajax.put(Constants.OPEN_ID, openId);
        return ajax;
    }


}
